﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditBrandCar : AdminPageBase
    {
        private ConcernInfo _concernInfo = new ConcernInfo();
        private readonly ConcernController _concernController = new ConcernController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ConcernId"]))
                _concernInfo = _concernController.GetConcern(int.Parse(Request.QueryString["ConcernId"]));

            if (Page.IsPostBack) return;
            LoadPartner();
            LoadStatus();
            LoadUser();
           
            if (_concernInfo.ConcernId == -1) return;

            
            RenderForm();
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListBrandCar.aspx?xml=BrandCar");
        }
        private void SaveData()
        {
            _concernInfo.ConcernFullName = txtConcernFullName.Text;
            ;
            _concernInfo.Status = int.Parse(ddlStatus.SelectedValue);

            

            _concernInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _concernInfo.ConcernType = 3;


            if (_concernInfo.ConcernId == -1)
            {
                _concernInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _concernInfo.CreatedDate = DateTime.Now;
                _concernController.InsertConcern(_concernInfo);
            }
            else
            {
                _concernInfo.UpdatedBy = UserId;
                _concernInfo.UpdatedDate = DateTime.Now;
                _concernController.UpdateConcern(_concernInfo);
            }

            Response.Redirect("/Pages/ListBrandCar.aspx?xml=BrandCar");
        }
        private void RenderForm()
        {
            txtConcernFullName.Text = _concernInfo.ConcernFullName;
           
            ddlStatus.SelectedValue = _concernInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _concernInfo.CreatedBy.ToString();
            ddlPartner.SelectedValue = _concernInfo.PartnerId.ToString();
        }
        private void LoadPartner()
        {
            ddlPartner.DataSource = new PartnerController().ListPartner()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.PartnerName).ToList();
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();

            ddlPartner.Items.Insert(0, new ListItem("--- Chọn nhà cung cấp ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}